import { setStore, getStore, removeStore } from '../../utils/store'
import axios from "axios"
const state = {
    user: null,
    isLogin: false,
}

// mutations
const mutations = {
    // 登录成功
    AUTH_LOGIN_SUCCESS(state, user) {
        state.isLogin = true
        state.user = user
        setStore('user_id', user.userId);
    },
    // 退出登录
    AUTH_LOGOUT_SUCCESS(state) {
        state.isLogin = false
        state.user = null
        removeStore('user_id')
    },
    // 自动登录,保存用户
    GET_USERINFO(state, info) {
        if (state.user && (state.user.userName !== info.data.userName)) {
            return;
        };
        if (info.code === 0) {
            state.isLogin = true
            state.user = info.data;
        } else {
            state.user = null;
        }
    },
}
// actions
const actions = {
    // 根据本地userId自动登录
    async getUser({ state, commit }) {
        const { data } = await axios.get("/pinduoduo/user/getUser", {
            params: {
                userId: getStore('user_id')
            }
        })
        commit('GET_USERINFO', data)
        console.log(data)
    },
}

// getters
const getters = {

}



export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}